#pragma once

template < class T, class Key = std::string >
/**
 * @brief ObjectRegistry holds all registry item of the same type
 *
 */
class ObjectRegistry
{
public:
    /**
     * @brief
     *
     */
    typedef std::map<Key, T*> RegistryMapType;

    /**
     * @brief Returns a registry item
     *
     * @param key
     * @return const T
     */
    const T* GetRegistryItem(Key key) const
    {
        typename RegistryMapType::const_iterator iter = i_registeredObjects.find(key);
        return(iter == i_registeredObjects.end() ? NULL : iter->second);
    }

    /**
     * @brief Inserts a registry item
     *
     * @param obj
     * @param key
     * @param replace
     * @return bool
     */
    bool InsertItem(T* obj, Key key, bool replace = false)
    {
        typename RegistryMapType::iterator iter = i_registeredObjects.find(key);
        if (iter != i_registeredObjects.end())
        {
            if (!replace)
            {
                return false;
            }
            delete iter->second;
            i_registeredObjects.erase(iter);
        }

        i_registeredObjects[key] = obj;
        return true;
    }

    /**
     * @brief Removes a registry item
     *
     * @param key
     * @param delete_object
     */
    void RemoveItem(Key key, bool delete_object = true)
    {
        typename RegistryMapType::iterator iter = i_registeredObjects.find(key);
        if (iter != i_registeredObjects.end())
        {
            if (delete_object)
            {
                delete iter->second;
            }
            i_registeredObjects.erase(iter);
        }
    }

    /**
     * @brief Returns true if registry contains an item
     *
     * @param key
     * @return bool
     */
    bool HasItem(Key key) const
    {
        return (i_registeredObjects.find(key) != i_registeredObjects.end());
    }

    /**
     * @brief Inefficiently return a vector of registered items
     *
     * @param l
     * @return unsigned int
     */
    unsigned int GetRegisteredItems(std::vector<Key>& l) const
    {
        unsigned int sz = l.size();
        l.resize(sz + i_registeredObjects.size());
        for (typename RegistryMapType::const_iterator iter = i_registeredObjects.begin(); iter != i_registeredObjects.end(); ++iter)
        {
            l[sz++] = iter->first;
        }
        return i_registeredObjects.size();
    }

    /**
     * @brief Return the map of registered items
     *
     * @return const RegistryMapType
     */
    RegistryMapType const& GetRegisteredItems() const
    {
        return i_registeredObjects;
    }

private:
    RegistryMapType i_registeredObjects; /**< TODO */
#if 0
    friend class MaNGOS::OperatorNew<ObjectRegistry<T, Key> >;
#endif

    /**
     * @brief protected for friend use since it should be a singleton
     *
     */
    ObjectRegistry() {}
    /**
     * @brief
     *
     */
    ~ObjectRegistry()
    {
        for (typename RegistryMapType::iterator iter = i_registeredObjects.begin(); iter != i_registeredObjects.end(); ++iter)
        {
            delete iter->second;
        }
        i_registeredObjects.clear();
    }
};